package tt.com.feesmanager.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tt.com.common.model.Dead;
import tt.com.common.model.DeadProject;
import tt.com.common.util.CommonResult;
import tt.com.feesmanager.Vo.deadprojectVo;
import tt.com.feesmanager.service.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping(value = "fee/Manager")
public class feesManagerControll {
    @Autowired
    private FeesService feesService;
    @Autowired
    private Benefitingpeople benefitingpeople;
    @Autowired
    private feeCollection feeCollection;
    @Autowired
    private feeConcessions concessions;
    @Autowired
    private FeeSettlementService feeSettlementService;
    @Autowired
    private ProcessApproval processApproval;
    @Autowired
    private RefundService refundService;
    @Autowired
    private SelectNeedbufei needbufei;

    //打印信息表
    @PostMapping(value = "createPDF")
    public ResponseEntity createPDF(@RequestBody DeadProject deadProject, HttpServletResponse response)throws DocumentException {
        //获取模板路径
        URL templatePath=this.getClass().getResource("/printTemplate/bill.pdf");
        //生成文件路径
        String filenname="billpdf.pdf";

        Map<String,Object> datas=new HashMap<String,Object>();
        datas.put("projectId",deadProject.getProjectId());
        datas.put("DeadId",deadProject.getDeadId());
        datas.put("InvoiceNumber",deadProject.getInvoiceNumber());
        datas.put("InventoryNumber",deadProject.getInventoryNumber());
        datas.put("UnitPrice",deadProject.getUnitPrice());
        datas.put("Count",deadProject.getCount());
        datas.put("Cost",deadProject.getCost());
        datas.put("FruitCost",deadProject.getFruitCost());
        datas.put("SubsidyPrice",deadProject.getSubsidyPrice());

        PdfReader reader;
        OutputStream os = null;
        ByteArrayOutputStream baos = null;
        PdfStamper stamper;

        try {
            os = response.getOutputStream();
            // 读入pdf表单
            reader = new PdfReader(templatePath);
            baos = new ByteArrayOutputStream();
            // 根据表单生成一个新的pdf
            stamper = new PdfStamper(reader, baos);
            // 获取pdf表单
            AcroFields formTexts = stamper.getAcroFields();

            // 设置字体(这里设置为系统字体，你也可以引入其他的字体)，不设置很可能，中文无法显示。
            BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/SIMSUN.TTC,1",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            formTexts.addSubstitutionFont(bf);

            //数据查询后，值的植入
            Set<String> ketSet=datas.keySet();
            for (String key : ketSet){
                formTexts.setField(key,datas.get(key).toString());
            }
            stamper.setFormFlattening(true);
            stamper.close();

            //创建一个PDF实例
            Document document=new Document();
            PdfCopy copy=new PdfCopy(document,os);
            document.open();
            PdfImportedPage importedPage = copy.getImportedPage(new PdfReader(baos.toByteArray()), 1);
            copy.addPage(importedPage);
            document.close(); // open和close一套

            //强制下载

            //response.reset();
            response.setContentType("application/pdf;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;fileName="
                    + URLEncoder.encode(filenname, "UTF-8"));
        }catch (IOException e){
            e.printStackTrace();

        }finally {
            try {
                baos.close();
                os.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return new ResponseEntity("成功", HttpStatus.OK);
    }

    //显示信息
    @PostMapping(value = "show/deadproject")
    public ResponseEntity showDeadProject(@RequestBody deadprojectVo vo, HttpServletRequest request){
        CommonResult result = feesService.showDeadProject(vo.getPageUtil());
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //删除
    @PostMapping(value = "delete/deadproject")
    public ResponseEntity deleteDeadProject(@RequestBody DeadProject deadProject,HttpServletRequest request){
        CommonResult result = feesService.deleteDeadproject(deadProject);
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //修改
    @PostMapping(value = "update/deadproject")
    public ResponseEntity updateDeadProject(@RequestBody DeadProject deadProject ,HttpServletRequest request){
        CommonResult result =feesService.updateDeadProject(deadProject);
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //查询
    @PostMapping(value = "select/deadproject")
    public ResponseEntity selectDeadProject(@RequestBody deadprojectVo vo,HttpServletRequest request){
        CommonResult result = feesService.selectDeadproject(vo.getDeadProject(),vo.getPageUtil());
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //惠民减免
    @PostMapping(value = "benepeople")
    public ResponseEntity BenePeople(@RequestBody DeadProject deadProject,HttpServletRequest request){
        CommonResult result = benefitingpeople.Benefiting(deadProject);
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //确认收款
    @PostMapping(value = "feecollection")
    public ResponseEntity feecollection(@RequestBody DeadProject deadProject){
        CommonResult result = feeCollection.feecollection(deadProject);
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //费用优惠
    @PostMapping(value = "fee/concessions")
    public ResponseEntity feeCons(@RequestBody DeadProject deadProject){
        CommonResult result = concessions.feeconcessions(deadProject);
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //费用结算
    @PostMapping(value = "fee/settlement")
    public ResponseEntity setlement(@RequestBody DeadProject deadProject){
        CommonResult result = feeSettlementService.FeeSettlement(deadProject);
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //流程审批
    @PostMapping(value = "process")
    public ResponseEntity ProcessApproval(@RequestBody DeadProject deadProject){
        CommonResult result = processApproval.ProcessApproval(deadProject);
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //退费
    @PostMapping(value = "refund")
    public ResponseEntity Refund(@RequestBody DeadProject deadProject){
        CommonResult result = refundService.Refund(deadProject);
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

    //查询需要补费的业务信息
    @PostMapping(value = "select/need")
    public ResponseEntity selectNeddBf(@RequestBody deadprojectVo vo,HttpServletRequest request){
        CommonResult result = needbufei.SelectNeedBufei(vo.getDeadProject(),vo.getPageUtil());
        return new ResponseEntity(result,HttpStatus.valueOf(result.getStatus()));
    }

}
